< previous page page_341 next page >

Page 341
e10a6a0a59f035e870d90cca05b85fce.gif
    ' Perform the Gaussian elimination.
    If GaussianEliminate(coeff) Then
        ' There is a solution.
        ' Save the results.
        ReDim a_values(0 To degree)
        For row = 0 To degree
            a_values(row) = coeff(row, degree + 1)
        Next row
        GetLeastSquaresValues = True
    Else
        ' There is no solution.
        GetLeastSquaresValues = False
    End If
End Function

' Perform Gaussian elimination on this array.
' Return True if there is a solution.
Private Function GaussianEliminate(coeff() As Single) _
    As Boolean
Dim max_row As Integer
Dim max_col As Integer
Dim row As Integer
Dim col As Integer
Dim i As Integer
Dim j As Integer
Dim factor As Single
Dim tmp As Single

    max_row = UBound(coeff, 1)
    max_col = UBound(coeff, 2)
    For row = 0 To max_row
        ' Make sure coeff(row, row) <> 0.
        factor = coeff(row, row)
        If Abs(factor) < 0.001 Then
            ' Switch this row with one that is not
            ' zero in position. Find this row.
            For i = row + 1 To max_row
                If Abs(coeff(i, row) > 0.001) Then
                    ' Switch rows i and row.
                    For j = 0 To max_col
                        tmp = coeff(row, j)
                        coeff(row, j) = coeff(i, j)
                        coeff(i, j) = tmp
                    Next j
                    factor = coeff(row, row)
                End If
            Next i

            ' See if we found a good row.
            If Abs(factor) < 0.001 Then
                ' We found no good row.

 
< previous page page_341 next page >